package com.rabbitmq.死信队列.过期时间;


import com.rabbitmq.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.springframework.amqp.rabbit.connection.RabbitUtils;
import org.springframework.amqp.support.ConsumerTagStrategy;

import java.util.HashMap;

/**
 * @Auther: liyanhao
 * @Description: 死信队列 -死信消费者01
 * @Date: 2022/5/11 21:53
 * @Version: v1.0
 */
public class Consumer01 {
    //普通交换机的名称
    public static final String NORMAL_EXCHANGE = "normal_exchange";
    //死信交换机的名称
    public static final String DEAD_EXCHANGE = "dead_exchange";
    //普通队列的名称
    public static final String NORMAL_QUEUE = "normal_queue";
    //死信队列的名称
    public static final String DEAD_QUEUE = "dead_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        //声明死信交换机和普通交换机 类型为 direct
        channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
        //声明死信和普通队列
        channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
        //声明普通队列
        HashMap<String, Object> arguments = new HashMap<>();
        //正常队列设置死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        //设置死信routingkey
        arguments.put("x-dead-letter-routing-key","lisi");
        channel.queueDeclare(NORMAL_QUEUE, false, false, false,arguments );

        //绑定普通的交换机和队列
        channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan");
        //绑定死信的交换机和队列
        channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
        System.out.println("等待接收消息...");
        DeliverCallback deliverCallback =((consumerTag, message) -> {
            System.out.println("Consumer01接收的消息是：" + new String(message.getBody(), "UTF-8"));
        });
        channel.basicConsume(NORMAL_QUEUE,true,deliverCallback,consumerTag -> {

        });

    }

}
